package com.kklldog.logindemo;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.kklldog.po.User;
import com.kklldog.util.Uuid;
import com.kklldog.util.Dialog;
import com.kklldog.services.UserService;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;

/** 
 * @author mj.zhou 
 * @version create：2013-12-3 下午4:03:48 
 * User Edit
 */
public class UserEditActivity extends Activity implements ValidationListener  {

	private User _user;
	
	@Required(order = 1, message="姓名不能为空")
    private EditText txtName=null;
	@Password(order = 2, message="密码不能为空")
	@TextRule(order = 3, minLength = 6, message = "至少6位")
	private EditText pswName=null;
	
	private RadioButton rbSexF=null;
	private RadioButton rbSexM=null;
	
	private Validator validator=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_edit);
		if (android.os.Build.VERSION.SDK_INT > 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}
		
		Intent intent = this.getIntent();
		_user = (User)intent.getSerializableExtra("user");//页面传参
		loadUser();//加载用户
		initListener();//初始化监听
		//实例化校验类库
		validator = new Validator(this);
		validator.setValidationListener(this);

	}

	/**
	 * 初始化监听
	 */
	private void initListener() {
		txtName= (EditText)this.findViewById(R.id.txtEditName);
		pswName = (EditText)this.findViewById(R.id.txtEditPassword);
		rbSexF= (RadioButton)this.findViewById(R.id.rbSexF);
		rbSexM= (RadioButton)this.findViewById(R.id.rbSexM);
		
		Button btn = (Button)this.findViewById(R.id.btnSave);
		btn.setOnClickListener(new OnClickListener(){

			//保存事件
			@Override
			public void onClick(View arg0) {
				
				validator.validate();

			}
			
		});
	}

	/**
	 * 加载用户
	 */
	private void loadUser()
	{
		if(_user!=null)
		{
			EditText txtName = (EditText)this.findViewById(R.id.txtEditName);
			EditText pswName = (EditText)this.findViewById(R.id.txtEditPassword);
			RadioButton rbSexF= (RadioButton)this.findViewById(R.id.rbSexF);
			RadioButton rbSexM= (RadioButton)this.findViewById(R.id.rbSexM);
			
			txtName.setText(_user.getName());
			pswName.setText(_user.getPassword());
	
			if(rbSexF.getText().equals(_user.getSex()))
			{
				rbSexF.setChecked(true);
			}
			else if(rbSexM.getText().equals(_user.getSex()))
			{
				rbSexM.setChecked(true);
			}
		}
	}
	private void save() {
		if(_user==null)
		{
			_user=new User();
		}
		_user.setName(txtName.getText().toString());
		_user.setPassword(pswName.getText().toString());
		if(rbSexF.isChecked())
		{
			_user.setSex(rbSexF.getText().toString());
		}
		else if	(rbSexM.isChecked())
		{
			_user.setSex(rbSexM.getText().toString());
		}
		UserService service = new UserService();
		if(_user.getId()==null)
		{
			// add
			_user.setId(Uuid.GetUUID());
			try {
				service.create(_user, User.class);
				Dialog.showToast("保存成功", this);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			//update
			try {
				service.update(_user);
				Dialog.showToast("保存成功", this);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
	
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{

		
		
		return true;
	}

	@Override
	public void preValidation() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSuccess() {
		// TODO Auto-generated method stub
		save();
	}



	@Override
	public void onFailure(View failedView, Rule<?> failedRule) {
		String message = failedRule.getFailureMessage();

        if (failedView instanceof EditText) {
            failedView.requestFocus();
            ((EditText) failedView).setError(message);
        } else {
            Dialog.showToast(message,this);
        }
	}

	@Override
	public void onValidationCancelled() {
		// TODO Auto-generated method stub
		
	}

}
